package cn.zebra.dev.member.module.address.controller;

import cn.zebra.dev.common.constant.ParamsConstant;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.member.module.address.dto.MemberAddressDTO;
import cn.zebra.dev.member.module.address.entity.MemberAddressEntity;
import cn.zebra.dev.member.module.address.service.MemberAddressService;
import cn.zebra.dev.member.module.address.vo.MemberAddressVO;
import cn.zebra.dev.member.module.login.service.SecurityMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "地址管理")
@RestController
@RequestMapping("/address")
public class MemberAddressController {

    @Autowired
    private SecurityMemberService securityMemberService;

    @Autowired
    private MemberAddressService memberAddressService;

    @GetMapping("/getList")
    @ApiOperation("我的收货地址列表")
    public Result<PageUtils<MemberAddressDTO>> getList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        Long memberId = securityMemberService.getMemberId();
        Map<String, Object> params = new HashMap<>();
        params.put(ParamsConstant.PAGE, page);
        params.put("memberId", memberId);
        params.put(ParamsConstant.LIMIT, limit);
        PageUtils<MemberAddressEntity> pagrUtils = memberAddressService.queryPage(params);
        List<MemberAddressEntity> list = pagrUtils.getList();
        List<MemberAddressDTO> list1 = new ArrayList<>();
        list.forEach(entity -> {
            MemberAddressDTO dto = new MemberAddressDTO();
            BeanUtils.copyProperties(entity, dto);
            list1.add(dto);
        });
        PageUtils<MemberAddressDTO> data = new PageUtils<>(pagrUtils);
        data.setList(list1);
        return Result.success(data);
    }

    @GetMapping("/get/{id}")
    @ApiOperation("地址详情")
    public Result<MemberAddressDTO> get(@PathVariable Long id) {
        MemberAddressEntity entity = memberAddressService.findByIdAndMemberId(id, securityMemberService.getMemberId());
        if (entity == null) {
            return Result.error("地址不存在");
        }
        MemberAddressDTO dto = new MemberAddressDTO();
        BeanUtils.copyProperties(entity, dto);
        return Result.success(dto);
    }

    @GetMapping("/isEmpty")
    @ApiOperation("判断用户地址是否为空")
    public Result isEmpty() {
        Long memberId = securityMemberService.getMemberId();
        return Result.success(memberAddressService.isEmptyMemberAddress(memberId));
    }

    @PostMapping("/save")
    @ApiOperation("添加新地址")
    @Transactional(rollbackFor = Exception.class)
    public Result add(@Valid @RequestBody MemberAddressVO vo) {
        Long memberId = securityMemberService.getMemberId();
        if (vo.getDefaultFlag()) {
            memberAddressService.cancelAllDefault(memberId);
        }
        MemberAddressEntity entity = new MemberAddressEntity();
        BeanUtils.copyProperties(vo, entity);
        entity.setMemberId(memberId);
        entity.setCreateTime(LocalDateTime.now());
        return Result.success(memberAddressService.save(entity));
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改地址")
    @Transactional(rollbackFor = Exception.class)
    public Result update(@Valid @PathVariable Long id, @RequestBody MemberAddressVO vo) {
        Long memberId = securityMemberService.getMemberId();
        if (vo.getDefaultFlag()) {
            memberAddressService.cancelAllDefault(memberId);
        }
        MemberAddressEntity entity = new MemberAddressEntity();
        BeanUtils.copyProperties(vo, entity);
        entity.setUpdateTime(LocalDateTime.now());
        return Result.success(memberAddressService.update(entity, "id = {0}", id));
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除地址")
    public Result delete(@PathVariable Long id) {
        Long memberId = securityMemberService.getMemberId();
        MemberAddressEntity entity = new MemberAddressEntity();
        entity.setDeleteFlag(true);
        return Result.success(memberAddressService.update(entity, "id = {0} and member_id = {1}", id, memberId));
    }
}
